﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Admin.Web.config;
using System.Text;
using com.huigo.Admin.Web.Config;

namespace com.huigo.Admin.Web.ctrl
{
    public partial class TopMenu : System.Web.UI.UserControl
    {
        int currentParentId;
        int currentId;
        MenuConfig mc;
        public string Username;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Username))
            {
                mc = BaseConfig.LoadMenuConfig(Username);
                currentParentId = BaseConfig.GetParentMenuId(mc, Request.Url.AbsolutePath);
                currentId = BaseConfig.GetMenuId(mc, Request.Url.AbsolutePath);
                LoadMenu();
                if (currentParentId == int.MinValue && currentId == int.MinValue)
                {
                    com.loto.Common.MessageBox.ShowAndRedirect(this.Parent.Page, "你没有访问该页面的权限，请联系系统管理员！", "/Default.aspx");
                }
            }
        }

        private void LoadMenu()
        {
            if (mc.MenuList != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<div id=\"topmenu\">");
                sb.AppendLine("<ul>");
                foreach (Config.Model.Menu ss in mc.MenuList)
                {
                    if (ss.ID == currentParentId)
                    {
                        sb.AppendLine("<li id='pmid-" + ss.ID + "' class=\"current\" onmouseover=\"menuHover('" + ss.ID + "')\"><a href=\"" + ss.Url + "\">" + ss.Name + "</a></li>");
                    }
                    else
                    {
                        sb.AppendLine("<li id='pmid-" + ss.ID + "' onmouseover=\"menuHover('" + ss.ID + "')\"><a href=\"" + ss.Url + "\">" + ss.Name + "</a></li>");
                    }
                }
                sb.AppendLine("</ul>");
                sb.AppendLine("</div>");
                sb.AppendLine("<div id=\"top-panel\">");
                sb.AppendLine("<div id=\"panel\">");
                foreach (Config.Model.Menu ss in mc.MenuList)
                {
                    if (ss.ID == currentParentId)
                    {
                        sb.AppendLine("<ul id='mid-" + ss.ID + "'>");
                        foreach (Config.Model.BaseMenu s in ss.ChildMenu)
                        {
                            if (currentId == s.ID)
                                sb.AppendLine("<li class=\"currentchild\"\"><a href=\"" + s.Url + "\">" + s.Name + "</a></li>");
                            else
                                sb.AppendLine("<li><a href=\"" + s.Url + "\">" + s.Name + "</a></li>");
                        }
                        sb.AppendLine("</ul>");
                    }
                    else
                    {
                        sb.AppendLine("<ul id='mid-" + ss.ID + "' style='display:none;'>");
                        foreach (Config.Model.BaseMenu s in ss.ChildMenu)
                        {
                            sb.AppendLine("<li><a href=\"" + s.Url + "\">" + s.Name + "</a></li>");
                        }
                        sb.AppendLine("</ul>");
                    }
                }
                sb.AppendLine("</div>");
                sb.AppendLine("</div>");
                litMenu.Text = sb.ToString();
            }
        }
    }
}